package com.maxd.controller.article;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.Article;
import com.maxd.model.ArticleClassify;
import com.maxd.respository.article.ArticleClassifyJpaRepository;
import com.maxd.respository.article.ArticleJpaRepository;
import com.maxd.respository.article.ArticleRepository;
import com.maxd.service.article.ArticleBrowsingHistoryService;
import com.maxd.service.article.ArticleClassifyService;
import com.maxd.service.article.ArticleCollectService;
import com.maxd.service.article.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: fang
 * \* Date: 2020/5/29
 * \* Time: 11:01
 * \*
 * \
 */
@RestController
@Api(value = "文章管理", tags = {"文章管理"})
@RequestMapping(value = "/article")
public class ArticleController {

    /** 浏览记录 */
    @Autowired
    private ArticleBrowsingHistoryService articleBrowsingHistoryService;
    /** 文章分类 */
    @Autowired
    private ArticleClassifyService articleClassifyService;
    /** 收藏 */
    @Autowired
    private ArticleCollectService articleCollectService;
    /** 文章 */
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleJpaRepository articleJpaRepository;
    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private ArticleClassifyJpaRepository articleClassifyJpaRepository;

    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    @ApiOperation("添加文章")
    @ResponseBody
    public Result addArticle(@RequestBody Article article) {
        articleService.save(article);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectArticleList", method = RequestMethod.GET)
    @ApiOperation("根据分类查询文章")
    @IgnoreSecurity
    @ResponseBody
    public Result selectArticleList(Integer page,Integer size,Long classifyId)
    {
        Pageable pageable=PageRequest.of(page,size);
        return ResultUtil.success(articleRepository.getArticleList(pageable,classifyId));
    }

    @IgnoreSecurity
    @RequestMapping(value = "/selectArticleById", method = RequestMethod.GET)
    @ApiOperation("根据文章id查看详细信息")
    @ResponseBody
    public Result selectArticleById(Long articleId)
    {
        return ResultUtil.success(articleService.selectArticleById(articleId));
    }

    @RequestMapping(value = "/deleteArticle", method = RequestMethod.POST)
    @ApiOperation("删除文章")
    @ResponseBody
    public Result deleteArticle(Long articleId) {
        articleJpaRepository.deleteById(articleId);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteArticleList", method = RequestMethod.POST)
    @ApiOperation("删除多个文章")
    @ResponseBody
    public Result deleteArticleList(String articleIds) {
        articleService.updateArticleStateByIds(articleIds);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectArticleClassifyList", method = RequestMethod.GET)
    @ApiOperation("查看所有分类")
    @ResponseBody
    @IgnoreSecurity
    public Result selectArticleClassifyList()
    {
        return ResultUtil.success(articleClassifyService.selectArticleClassifyList());
    }

    @RequestMapping(value = "/saveArticleClassify", method = RequestMethod.POST)
    @ApiOperation("添加分类")
    @ResponseBody
    public Result saveArticleClassify(@RequestBody ArticleClassify articleClassify) {
        articleClassifyService.save(articleClassify);

        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteArticleClassifyById", method = RequestMethod.POST)
    @ApiOperation("删除分类")
    @ResponseBody
    public Result deleteArticleClassifyById(Long articleClassifyId) {
        articleClassifyJpaRepository.deleteById(articleClassifyId);
//        articleClassifyService.updateStateByArticleClassifyId(articleClassifyId,2);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteArticleClassifyByIds", method = RequestMethod.POST)
    @ApiOperation("删除多个分类")
    @ResponseBody
    public Result deleteArticleClassifyByIds(String articleClassifyIds) {
        articleClassifyService.updateStateByArticleClassifyIds(articleClassifyIds);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/updateClassifyName", method = RequestMethod.POST)
    @ApiOperation("修改分类名称")
    @ResponseBody
    public Result updateClassifyName(Long articleClassifyId,String articleClassifyName) {
        articleClassifyService.updateClassifyNameByArticleClassifyId(articleClassifyId,articleClassifyName);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/updateClassifySort", method = RequestMethod.POST)
    @ApiOperation("修改分类顺序")
    @ResponseBody
    public Result updateClassifySort(Long articleClassifyId,Integer sort) {
        articleClassifyService.updateSortByArticleClassifyId(articleClassifyId,sort);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectArticleBrowsingHistoryList", method = RequestMethod.GET)
    @ApiOperation("查看所有浏览记录")
    @ResponseBody
    public Result selectArticleClassifyList(Long userId,int page,int size)
    {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(articleBrowsingHistoryService.selectArticleBrowsingHistoryList(userId,pageable));
    }


    @RequestMapping(value = "/selectArticleCollectList", method = RequestMethod.GET)
    @ApiOperation("查看所有收藏记录")
    @ResponseBody
    public Result selectArticleCollectList(Long userId,int page,int size)
    {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(articleCollectService.selectArticleCollectList(userId,pageable));
    }

    @RequestMapping(value = "/updateArticleCollect", method = RequestMethod.POST)
    @ApiOperation("收藏和取消收藏")
    @ResponseBody
    public Result updateArticleCollect(Long userId,Long articleCollectId)
    {
        articleCollectService.updateArticleCollect(userId,articleCollectId);
        return ResultUtil.success();
    }




}